﻿#region - Class details -
/*  
 *  Developer   : Wicus Botha
 *  Description : 
 *  History     : WB - 2008-10-20 - Created
 */
#endregion

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using IQ.DbBase;

namespace SqlServerAdmin
{
    public class SqlSpVars
    {
        public delegate void Progress(int position);
        public event Progress SetMaxPosition;
        public event Progress SetCurrentPosition;
        #region # Constructor #
        public SqlSpVars()
        {
            createResultsTable();
        }
        #endregion

        #region - Private variables -
        private DataTable dtSp;
        private DataTable dtSpVars;
        #endregion

        #region - Private methods -
        private void loadStoreProcNames()
        { 
            dtSp = DbHelper.Load_Table(SqlQueries.SqlProcedures);
        }
        private void createResultsTable()
        {
            dtSpVars = new DataTable();
            DbHelper.AddDataColumn(dtSpVars, "SpName", "System.String", false, " ");
            DbHelper.AddDataColumn(dtSpVars, "VarName", "System.String", false, " ");
            DbHelper.AddDataColumn(dtSpVars, "VarDataType", "System.String", false, " ");
            DbHelper.AddDataColumn(dtSpVars, "VarValue", "System.String", false, " ");
            DbHelper.AddDataColumn(dtSpVars, "VarSection", "System.String", false, " ");
            DbHelper.AddDataColumn(dtSpVars, "VarText", "System.String", false, " ");
        }
        private void doCurrentStoreProc(string spName)
        {
            DataTable dtSpText = DbHelper.Load_Table(SqlQueries.SqlProcDetail + "OBJECT_ID('" + spName + "')");
            foreach (DataRow dr in dtSpText.Rows)
            {
                processSpText(spName, dr["ObjectText"].ToString());
            }
        }
        private void processSpText(string spName, string rowText)
        {
            int iStart = rowText.IndexOf("DECLARE");
            int iAt = rowText.IndexOf("DECLARE", iStart);
            int iAtStart = iAt;
            
                iAt = rowText.IndexOf('@');
                int iBegin = rowText.IndexOf("BEGIN");
                if (iBegin > iAt)
                {
                    doVarList(rowText.Substring(iAt, iBegin),"");
                }
            // Get the index of "BEGIN" any var before this will be a input param
        }
        private void doVarList(string varlist, string section)
        {
            string[] vars = varlist.Trim().Split(',');
            foreach (string varText in vars)
            {
                //int iSpc =
                //string atVar = varText.Substring(
            }
        }
        #endregion

        #region - Public properties / variables -
        #endregion

        #region - Public methods -
        public void FindSpVariables()
        {
            loadStoreProcNames();
            createResultsTable();
            if (SetMaxPosition != null)
                SetMaxPosition(dtSp.Rows.Count);
            for (int i = 0; i < dtSp.Rows.Count; ++i)
            {
                //
                doCurrentStoreProc(dtSp.Rows[i]["Name"].ToString());
                //
                if (SetCurrentPosition != null)
                    SetCurrentPosition(i);
            }
        }
        #endregion
    }
}
